package br.com.exemplo.swing;

import java.awt.Color;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.LineBorder;


public class ExemploJTableComClique {

	public static void main(String[] args) {
		JFrame janela = new JFrame("Titulo da Janela");

		JPanel painel = new JPanel();
		janela.add(painel);

		final JTable tabela = new JTable();

		tabela.setBorder(new LineBorder(Color.BLACK));
		tabela.setGridColor(Color.BLACK);
		tabela.setShowGrid(true);

		final ArrayList<String> nomes = new ArrayList<String>();
		nomes.add("AAAA");
		nomes.add("BBBB");
		nomes.add("CCCC");
		nomes.add("DDDD");
		nomes.add("EEEE");
		nomes.add("FFFF");

		final ConteudoTabela conteudoTabela = new ConteudoTabela(nomes);
		tabela.setModel(conteudoTabela);

		tabela.addMouseListener(new java.awt.event.MouseAdapter() {
			@Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				int row = tabela.rowAtPoint(evt.getPoint());
				int col = tabela.columnAtPoint(evt.getPoint());
				// JOptionPane.showMessageDialog(null, "Linha: " + row
				// + " Coluna: " + col);

				nomes.add("Nome " + nomes.size());

				conteudoTabela.fireTableDataChanged();

				JFrame jFrame = new JFrame();
				JPanel jPanel = new JPanel();
				jFrame.add(jPanel);

				jPanel.add(new JLabel("Linha: " + row + " Coluna: " + col));

				jFrame.setVisible(true);
				jFrame.pack();
			}
		});

		JScrollPane scroll = new JScrollPane();
		scroll.getViewport().setBorder(null);
		scroll.getViewport().add(tabela);
		scroll.setSize(450, 450);
		painel.add(scroll);

		janela.setSize(500, 500);
		janela.setVisible(true);
		janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

}
